package org.service.car.activity;

import java.util.List;

import org.service.car.R;
import org.service.car.database.Util;
import org.service.car.hard.code.CategoriaType;
import org.service.car.pojo.Fornecedor;

import android.content.Context;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapsInitializer;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

public class MapaTeste extends FragmentActivity implements LocationListener {

	private static final Integer TWO_MINUTES = 1000 * 120;
	private GoogleMap map;
	private SupportMapFragment fragment;
	private List<Fornecedor> fornecedores = null;
	private LocationManager locationManager = null;
	private Location lastKnowLocation = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_mapa_fornecedores);
		
		fornecedores = Util.getAllFornecedorByEspecialidade(
				getApplicationContext(), CategoriaType.getIds());

		fragment = (SupportMapFragment) getSupportFragmentManager()
				.findFragmentById(R.id.map);
		map = fragment.getMap();
		
		locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);

		lastKnowLocation = locationManager
				.getLastKnownLocation(getBestProvider());

		if (lastKnowLocation != null) {
			onLocationChanged(lastKnowLocation);
		}
	  

		locationManager.requestLocationUpdates(getBestProvider(), TWO_MINUTES,
				200, this);

	}
	
	@SuppressWarnings("unchecked")
	@Override
	protected void onResume() {
		AdicionarFornecedores add = new AdicionarFornecedores();
		add.execute(fornecedores);
		super.onResume();
	}

	@Override
	public void onLocationChanged(Location location) {
		Double latitude = location.getLatitude();
		Double longitude = location.getLongitude();
		LatLng latLng = new LatLng(latitude, longitude);

		map.moveCamera(CameraUpdateFactory.newLatLng(latLng));
		map.animateCamera(CameraUpdateFactory.zoomTo(12));
	}

	@Override
	public void onProviderDisabled(String provider) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onProviderEnabled(String provider) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onStatusChanged(String provider, int status, Bundle extras) {
		// TODO Auto-generated method stub

	}

	private String getBestProvider() {
		Criteria criteria = new Criteria();
		criteria.setAccuracy(Criteria.ACCURACY_COARSE);
		criteria.setAltitudeRequired(false);
		criteria.setCostAllowed(true);
		criteria.setPowerRequirement(Criteria.POWER_HIGH);
		String provider = this.locationManager.getBestProvider(criteria, true);
		return provider;
	}

	private class AdicionarFornecedores extends
			AsyncTask<List<Fornecedor>, Integer, List<Fornecedor>> {

		@Override
		protected List<Fornecedor> doInBackground(List<Fornecedor>... params) {
			return fornecedores;
		}

		@Override
		protected void onPostExecute(List<Fornecedor> result) {
			for (int i = 0; i < result.size(); i++) {
				Fornecedor fornecedor = result.get(i);
				MarkerOptions markerOptions = new MarkerOptions()
						.position(new LatLng(fornecedor.getLatitude(),
								fornecedor.getLongitude()))
								.snippet(fornecedor.getSnnipet() + "\n" + fornecedor.getUrl())
								.title(fornecedor.getNome());
				map.addMarker(markerOptions);
			}
		}
	}
}
